/**
* Created by jinxs on 14-9-1.
*/
(function ($) {
    /**
    * 缓存名称
    * @type {string}
    * @private
    */
    var _cacheName = 'uiSearchbox';
    /**
    * @name uiSearchbox
    * @class 搜索框
    * @constructor uiSearchbox
    * @description 构造函数.
    * @param options 组件配置参数：{}
    * @example
    *  //采用默认值
    * $('#scb').uiSearchbox();
    * //设置组件参数
    *  $('#scb').uiSearchbox({height:'20',width:'auto'});
    * //调用组件方法
    * $('#scb').uiSearchbox('clear','');
    */
    $.fn.uiSearchbox = function (options, param) {
        if (typeof options == "string") {
            return $.fn.uiSearchbox.methods[options](this, param);
        }
        return this.each(function(){
            var me = $(this);
            var state = me.data(_cacheName);

            options = options || {};
            if (state) {
                $.extend(state.options, options);
            } else {
                state = {
                    options: $.extend({}, $.fn.uiSearchbox.defaults, me.parseUIConfig(),options)
                };
                me.data(_cacheName, state);
                _init(me);
            }
            _setPrompt(me);
            _bindEvent(me);
            _setDisable(me, state.options.disabled);
            _setWidth(me);
        });
    };
    /**
    * 方法
    * @lends uiSearchbox.prototype
    */
    $.fn.uiSearchbox.methods = {
        /**
        * @summary 获取组件配置参数
        * @function
        * @name uiSearchbox#options
        * @returns {Object}
        * @example
        *  $('#btn').uiSearchbox('options');
        */
        options: function (target) {
            return $.data(target[0], _cacheName).options;
        },
        /**
        * @summary 获取组件菜单
        * @function
        * @name uiSearchbox#menu
        * @returns {Object} 菜单
        */
        menu: function (target) {
            return $.data(target[0], _cacheName).menu;
        },
        /**
        * @summary 获取组件的文本框
        * @function
        * @name uiSearchbox#textbox
        * @returns {Object} 文本框
        */
        textbox: function (target) {
            var targetID = target.getElemID();
            var input = $.getByParentID(targetID, $.uiTemplate.uiSearchbox.searchText);
            return input;
        },
        /**
        * @summary 获取组件的值
        * @function
        * @name uiSearchbox#getValue
        * @returns {String} 
        */
        getValue: function (target) {
            return $.data(target[0], _cacheName).options.value;
        },
        /**
        * @summary 设置组件的值
        * @name uiSearchbox#setValues
        * @function
        * @param values {Array<String>} 组件的值列表
        * @returns {jQuery} 组件对象
        */
        setValue: function (target, value) {
            target.uiSearchbox("options").value = value;
            target.uiSearchbox("textbox").val(value);
            target.uiSearchbox("textbox").blur();
            return target;
        },
        /**
        * @summary 清除组件的值
        * @name uiSearchbox#clear
        * @function
        * @returns {jQuery} 组件对象
        */
        clear: function (target) {
            target.uiSearchbox("setValue", "");
            return target;
        },
        /**
        * @summary 重置组件的值
        * @name uiSearchbox#reset
        * @function
        * @returns {jQuery} 组件对象
        */
        reset: function (target) {
            var options = $(this).uiSearchbox("options");
            target.uiSearchbox("setValue", options.originalValue);
            return target;
        },
        /**
        * @summary 获取组件搜索文本框的控件名
        * @function
        * @name uiSearchbox#getName
        * @returns {String} 
        */
        getName: function (target) {
            var targetID = target.getElemID();
            var input = $.getByParentID(targetID, $.uiTemplate.uiSearchbox.searchText);
            return input.attr('name');
        },
        /**
        * @summary 移除并返回组件
        * @name uiSearchbox#destroy
        * @function
        * @returns {jQuery} 组件对象
        */
        destroy: function (target) {
            var targetID = target.getElemID();
            var searchbox = $.getByParentID(targetID, $.uiTemplate.uiSearchbox.searchSapn);
            searchbox.remove();
            target.remove();
            return target;
        },
        /**
        * @summary 设置控件的宽度
        * @name uiSearchbox#resize
        * @function
        * @param width {Number} 宽度大小
        * @returns {jQuery} 方法调用者
        */
        resize: function (target, width) {
            _setWidth(target, width);
            return target;
        },
        /**
        * @summary 禁用组件
        * @name uiSearchbox#disable
        * @function
        * @returns {jQuery} 组件对象
        */
        disable: function (target) {
            _setDisable(target, true);
            _bindEvent(target);
            return target;
        },
        /**
        * @summary 启用组件
        * @name uiSearchbox#enable
        * @function
        * @returns {jQuery} 组件对象
        */
        enable: function (target) {
            _setDisable(target, false);
            _bindEvent(target);
            return target;
        }
    };
    /**
    * 默认配置
    * @lends uiSearchbox.prototype
    */
    $.fn.uiSearchbox.defaults = {
        /**
        * @type String
        * @default auto
        * @summary 组件宽度
        */
        width: "auto",
        /**
        * @type Number
        * @default 20
        * @summary 组件高度
        */
        height: 20,
        searchBtnRight:true,
        searchBtnClass:"",
        /**
        * @type String
        * @default ""
        * @summary 组件的提示信息
        */
        prompt: "",
        /**
         * @type String
         * @default ""
         * @summary 提示信息样式
         */
        Style:"",
        /**
        * @type String
        * @default ""
        * @summary 组件的值
        */
        value: "",
        /**
        * @type Object
        * @default null
        * @summary 组件的菜单
        */
        menu: null,
        /**
        * @type Boolean
        * @default false
        * @summary 指示组件是否禁用
        */
        disabled: false,
        /**
        * @summary 在组件值改变时触发的方法。
        * @event
        * @name uiSearchbox#searcher
        * @param value {String} 组件的值
        * @default emptyFn
        */
        searcher: function (value) {
        }
    };
    var _init = function (target) {
        var state = $.data(target[0], _cacheName);
        var options = state.options;
        target.addClass("ui_searchboxOrigin").hide();
        target.setElemID();
        var reg = new RegExp('({search})', 'g');
        var targetID = target.getElemID();
        $($.uiTemplate.uiSearchbox.html.replace(reg, targetID)).insertAfter(target);

        var name = target.attr("name");
        var input = $.getByParentID(targetID, $.uiTemplate.uiSearchbox.searchText);
        if (name) {
            input.attr("name", name);
            target.removeAttr("name").attr("searchboxName", name);
        }
        var searchbox = $.getByParentID(targetID, $.uiTemplate.uiSearchbox.searchSapn);
        searchbox.addClass(options.style);
        if(!options.searchBtnRight){
            searchbox.append(input.detach());
        }
        var btn = $.getByParentID(targetID, $.uiTemplate.uiSearchbox.searchBtn);
        btn.addClass(options.searchBtnClass);
    };
    var _setPrompt = function (target) {
        var state = $.data(target[0], _cacheName);
        var targetID = target.getElemID();
        var options = state.options;
        var input = $.getByParentID(targetID, $.uiTemplate.uiSearchbox.searchText);
        options.originalValue = options.value;
        if (options.value) {
            input.val(options.value);
            input.removeClass("ui_searchbox-prompt");
        } else {
            input.val(options.prompt);
            input.addClass("ui_searchbox-prompt");
        }
    };
    var _bindEvent = function (target) {
        var state = $.data(target[0], _cacheName);
        var options = state.options;
        var targetID = target.getElemID();
        var searchbox = $.getByParentID(targetID, $.uiTemplate.uiSearchbox.searchSapn);
        var input = $.getByParentID(targetID, $.uiTemplate.uiSearchbox.searchText);
        var btn = $.getByParentID(targetID, $.uiTemplate.uiSearchbox.searchBtn);
        input.unbind(".uiSearchbox");
        btn.unbind(".uiSearchbox");
        if (!options.disabled) {
            input.bind("blur.uiSearchbox", function (e) {
                searchbox.removeClass('ui_searchbox-hover');
                options.value = $(this).val();
                if (options.value == "") {
                    $(this).val(options.prompt);
                    $(this).addClass("ui_searchbox-prompt");
                } else {
                    $(this).removeClass("ui_searchbox-prompt");
                }
            }).bind("focus.uiSearchbox", function (e) {
                searchbox.addClass('ui_searchbox-hover');
                if ($(this).val() != options.value) {
                    $(this).val(options.value);
                }
                $(this).removeClass("ui_searchbox-prompt");
            }).bind("keydown.uiSearchbox", function (e) {
                if (e.which == $.keyCode.ENTER) {
                    e.preventDefault();
                    options.value = $(this).val();
                    target.val(options.value);
                    options.searcher.call(target, options.value);
                    return false;
                }
            });
            btn.bind("click.uiSearchbox", function () {
                target.val(options.value);
                options.searcher.call(target, options.value);
            }).bind("mouseenter.uiSearchbox", function () {
                $(this).addClass("ui_searchbox-button-hover");
            }).bind("mouseleave.uiSearchbox", function () {
                $(this).removeClass("ui_searchbox-button-hover");
            });
        }
    };

    var _setWidth = function (target, width) {
        var options = $.data(target[0], _cacheName).options;
        var targetID = target.getElemID();
        var searchbox = $.getByParentID(targetID, $.uiTemplate.uiSearchbox.searchSapn);
        if (width) {
            options.width = width;
        }
        if (options.width) {
            searchbox.width(options.width);
        }else{
            options.width = searchbox.outerWidth();
        }
        var input = $.getByParentID(targetID, $.uiTemplate.uiSearchbox.searchText);
        var btn = $.getByParentID(targetID, $.uiTemplate.uiSearchbox.searchBtn);
        var _b = searchbox.find("a.uiSearchbox-menu");
        input.outerWidth(searchbox.width() - _b.outerWidth() - btn.outerWidth())
            .outerHeight(options.height).css('line-height', options.height + 'px');
        _b.outerHeight(options.height);
        btn.outerHeight(options.height);
        var _d = _b.find("span.l-btn-left");
        _d.outerHeight(options.height);
        _d.find("span.l-btn-text").css({ height: _d.height() + "px", lineHeight: _d.height() + "px" });
    };
    var _setDisable = function (target, disable) {
        var state = $.data(target[0], _cacheName);
        var options = state.options;
        var targetID = target.getElemID();
        var input = $.getByParentID(targetID, $.uiTemplate.uiSearchbox.searchText);
        if (disable) {
            options.disabled = true;
            target.attr("disabled", true);
            input.attr("disabled", true);
        } else {
            options.disabled = false;
            target.removeAttr("disabled");
            input.removeAttr("disabled");
        }
    };
})(jQuery);